// -----------------------------------------------------------
// Game Framework
// Copyright © 2024-2024 deadcycle. All rights reserved.
// Homepage: https://deadcycle.gitee.io
// Feedback: deadcycle@163.com
// -----------------------------------------------------------

using UnityEditor;
using UnityEditor.UI;

using UnityEngine;

namespace UIWidget.Editor
{
    [CustomEditor(typeof(ButtonEx))]
    public class ButtonExInspector : ButtonEditor
    {
        private SerializedProperty m_EventTypeProperty;
        private SerializedProperty m_EnablePressRepeatProperty;
        private SerializedProperty m_PressRepeatIntervalProperty;
        private SerializedProperty m_HoldThresholdProperty;
        private SerializedProperty m_PressCheckIntervalProperty;

        protected override void OnEnable()
        {
            base.OnEnable();

            serializedObject.Update();
            m_EventTypeProperty = serializedObject.FindProperty("m_EventType");
            m_EnablePressRepeatProperty = serializedObject.FindProperty("m_EnablePressRepeat");
            m_PressRepeatIntervalProperty = serializedObject.FindProperty("m_PressRepeatInterval");
            m_HoldThresholdProperty = serializedObject.FindProperty("m_HoldThreshold");
            m_PressCheckIntervalProperty = serializedObject.FindProperty("m_PressCheckInterval");
        }

        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();

            EditorGUILayout.Space(10);

            using (new EditorGUILayout.VerticalScope(EditorStyles.helpBox))
            {
                using (new EditorGUILayout.HorizontalScope(EditorStyles.toolbar))
                {
                    GUILayout.Label("额外配置", GUIStyleHelper.Caption);
                }

                // 绘制事件枚举
                EditorGUI.BeginChangeCheck();
                ButtonEx.EventType eventType = (ButtonEx.EventType)m_EventTypeProperty.intValue;

                ButtonEx.EventType currentEventType =
                    (ButtonEx.EventType)EditorGUILayout.EnumFlagsField("触发事件方式", eventType);

                bool canPress = (currentEventType & ButtonEx.EventType.Press) != 0;
                bool canHold = (currentEventType & ButtonEx.EventType.Hold) != 0;

                if (canPress || canHold)
                {
                    EditorGUILayout.PropertyField(m_PressCheckIntervalProperty,
                        new GUIContent("Press/Hold 检测间隔(毫秒)"));
                }

                if (canPress)
                {
                    EditorGUILayout.PropertyField(m_EnablePressRepeatProperty,
                        new GUIContent("允许重复触发Press"));

                    if (m_EnablePressRepeatProperty.boolValue)
                    {
                        EditorGUI.indentLevel++;

                        EditorGUILayout.PropertyField(m_PressRepeatIntervalProperty,
                            new GUIContent("重复触发Press的间隔(秒)"));
                    }
                }

                if (canHold)
                {
                    EditorGUILayout.PropertyField(m_HoldThresholdProperty,
                        new GUIContent("触发Hold的阈值(秒)"));
                }

                if (EditorGUI.EndChangeCheck())
                {
                    m_EventTypeProperty.intValue = (int)currentEventType;
                    serializedObject.ApplyModifiedProperties();
                }
            }
        }
    }
}